{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "60eba67b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 作业：\n",
    "\n",
    "# 使用逻辑回归算法对鸢尾花数据集（或其他数据集）建模进行分类预测\n",
    "\n",
    "# 按照序号1-6，完成要求\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9ca61f61",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d20d7283",
   "metadata": {},
   "source": [
    "1.导入数据（10分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "59c433ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "data =  load_iris()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "182e727f",
   "metadata": {},
   "source": [
    "2.切分数据集（10分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cf3867ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 150 entries, 0 to 149\n",
      "Data columns (total 5 columns):\n",
      " #   Column             Non-Null Count  Dtype  \n",
      "---  ------             --------------  -----  \n",
      " 0   sepal length (cm)  150 non-null    float64\n",
      " 1   sepal width (cm)   150 non-null    float64\n",
      " 2   petal length (cm)  150 non-null    float64\n",
      " 3   petal width (cm)   150 non-null    float64\n",
      " 4   target             150 non-null    int32  \n",
      "dtypes: float64(4), int32(1)\n",
      "memory usage: 5.4 KB\n"
     ]
    }
   ],
   "source": [
    "feature_names = ['sepal length (cm)',\n",
    "  'sepal width (cm)',\n",
    "  'petal length (cm)',\n",
    "  'petal width (cm)','target']\n",
    "\n",
    "target_names = pd.DataFrame(['setosa', 'versicolor', 'virginica'])\n",
    "df = pd.concat([pd.DataFrame(data.data),pd.DataFrame(data.target)],axis=1)\n",
    "df.columns = feature_names\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0f949e8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((105, 4), (105,), (45, 4), (45,))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#拆分测试集数据 和 训练集数据\n",
    "X_train,X_test,Y_train,Y_test = train_test_split(df.iloc[:,:-1],df.iloc[:,-1],test_size=0.3,random_state=450)\n",
    "X_train.shape,Y_train.shape,X_test.shape,Y_test.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "421d9d4b",
   "metadata": {},
   "source": [
    "3.使用标准化包，对训练集来学习，从而对训练集和测试集来做标准化（20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e50b6699",
   "metadata": {},
   "outputs": [],
   "source": [
    "std = StandardScaler().fit(X_train)\n",
    "X_train = std.transform(X_train)\n",
    "X_test = std.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02bf6471",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "43e336ce",
   "metadata": {},
   "source": [
    "4.在确定l2范式的情况下，使用网格搜索判断solver, C的最优组合（20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4f491944",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, estimator=LogisticRegression(max_iter=10000),\n",
       "             param_grid={'C': [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35,\n",
       "                               0.39999999999999997, 0.44999999999999996,\n",
       "                               0.49999999999999994, 0.5499999999999999, 0.6,\n",
       "                               0.65, 0.7, 0.75, 0.7999999999999999, 0.85, 0.9,\n",
       "                               0.95, 1.0],\n",
       "                         'solver': ['liblinear', 'sag', 'newton-cg', 'lbfgs']})"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = {\n",
    "    'C':list(np.linspace(0.05,1,20)),\n",
    "    'solver':['liblinear','sag','newton-cg','lbfgs']\n",
    "}\n",
    "\n",
    "model = LogisticRegression(penalty='l2',max_iter=10000)\n",
    "\n",
    "GS = GridSearchCV(model,p,cv=5)\n",
    "GS.fit(X_train,Y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1babbd1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9619047619047618"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GS.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "3099c929",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 0.7999999999999999, 'solver': 'sag'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GS.best_params_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d21eef0",
   "metadata": {},
   "source": [
    "5.将最优的结果重新用来实例化模型，查看训练集和测试集下的分数（20分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fbede4c",
   "metadata": {},
   "source": [
    "(注意多分类需要增加参数  average='micro')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e7de0763",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = LogisticRegression(penalty='l2',max_iter=10000,C=GS.best_params_['C'],solver=GS.best_params_['solver'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "203975cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=0.7999999999999999, max_iter=10000, solver='sag')"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = model.fit(X_train,Y_train)\n",
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "da4284c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 2, 1, 0, 0, 0, 1, 0, 0, 2, 2, 0, 0, 2, 0, 2, 2, 1, 2, 1, 2,\n",
       "       2, 1, 1, 2, 0, 0, 1, 0, 1, 1, 1, 2, 1, 0, 0, 0, 1, 1, 1, 1, 2, 1,\n",
       "       1])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d57b0624",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.9714285714285714, 0.9333333333333333)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#打印 训练集  和  测试集 的得分\n",
    "\n",
    "model.score(X_train,Y_train),model.score(X_test,Y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "544c0523",
   "metadata": {},
   "source": [
    "6.计算精准率（20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5b29fe1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import recall_score\n",
    "from sklearn.metrics import f1_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "4c88d8eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "ac = accuracy_score(model.predict(X_test),Y_test)\n",
    "re = recall_score(model.predict(X_test),Y_test,average='micro')\n",
    "f1 = f1_score(model.predict(X_test),Y_test,average='micro')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b0a1b74d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.9333333333333333, 0.9333333333333333, 0.9333333333333333)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ac,re,f1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adcd2a21",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
